Page({
  data: {
    work: {},          // 作品详情数据
    commentContent: '', // 评论输入内容
    showCommentInput: false
  },

  onLoad(options) {
    // 获取从mine页传递的作品ID
    const workId = parseInt(options.id);
    console.log(workId+'--------------')
    // 模拟获取作品详情数据（实际项目中应调用接口）
    this.getWorkDetail(workId);
  },

  // 获取作品详情（模拟接口请求）
  getWorkDetail(workId) {
    // 模拟作品数据库
    const mockWorks = [
      {
        id: 101,
        avatar: 'https://picsum.photos/100/100?random=1',
        username: '西瓜',
        userId: 1, // 添加用户ID
        publishTime: '2小时前',
        content: '今天尝试了新的戚风蛋糕配方，口感超级松软！分享给大家～ #烘焙 #戚风蛋糕',
        images: [
          'https://picsum.photos/600/400?random=10',
          'https://picsum.photos/600/400?random=11',
          'https://picsum.photos/600/400?random=12'
        ],
        likeCount: 256,
        commentCount: 32,
        viewCount: 1250,
        isLiked: false,
        comments: [
          {
            id: 101,
            avatar: 'https://picsum.photos/100/100?random=10',
            username: '甜点爱好者',
            userId: 2, // 添加用户ID
            time: '1小时前',
            content: '看起来好成功！请问配方可以分享吗？',
            likeCount: 15,
            isLiked: false
          },
          {
            id: 102,
            avatar: 'https://picsum.photos/100/100?random=11',
            username: '烘焙新手',
            userId: 3, // 添加用户ID
            time: '1.5小时前',
            content: '蛋白霜怎么打发才能稳定？总是消泡',
            likeCount: 8,
            isLiked: false
          }
        ]
      },
      {
        id: 102,
        avatar: 'https://picsum.photos/100/100?random=2',
        username: '西瓜',
        userId: 1, // 添加用户ID
        publishTime: '5小时前',
        content: '周末在家做了蜂蜜蓝莓松饼，用了新鲜的蓝莓，口感超级棒～',
        images: [
          'https://picsum.photos/600/400?random=13'
        ],
        likeCount: 189,
        commentCount: 24,
        viewCount: 980,
        isLiked: false,
        comments: [
          {
            id: 201,
            avatar: 'https://picsum.photos/100/100?random=20',
            username: '家常菜爱好者',
            userId: 2, // 添加用户ID
            time: '4小时前',
            content: '蓝莓是新鲜的吗？还是冷冻的？',
            likeCount: 22,
            isLiked: false
          }
        ]
      },
      {
        id: 103,
        avatar: 'https://picsum.photos/100/100?random=3',
        username: '西瓜',
        userId: 1, // 添加用户ID
        publishTime: '昨天 18:30',
        content: '分享一道超好吃的红莓裸蛋糕，食材简单但营养丰富，特别适合夏天～',
        images: [
          'https://picsum.photos/600/400?random=14',
          'https://picsum.photos/600/400?random=15'
        ],
        likeCount: 124,
        commentCount: 18,
        viewCount: 750,
        isLiked: true,
        comments: [
          {
            id: 301,
            avatar: 'https://picsum.photos/100/100?random=30',
            username: '健康生活',
            userId: 3, // 添加用户ID
            time: '昨天 19:00',
            content: '这个蛋糕的奶油是什么做的？',
            likeCount: 12,
            isLiked: false
          }
        ]
      }
    ];

    // 根据ID查找作品
    const work = mockWorks.find(item => item.id === workId) || mockWorks[0];
    
    this.setData({
      work: work
    });
  },

  // 返回上一页
  navigateBack() {
    wx.navigateBack();
  },

  // 点击作者头像 - 跳转到用户详情页
  onAuthorAvatarTap() {
    const userId = this.data.work.userId || 1;
    wx.navigateTo({
      url: `/my/pages/user-detail/user-detail?id=${userId}`
    });
  },

  // 点击评论用户头像 - 跳转到用户详情页
  onCommentUserAvatarTap(e) {
    const userId = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: `/my/pages/user-detail/user-detail?id=${userId}`
    });
  },

  // 点赞
  handleLike() {
    const work = { ...this.data.work };
    work.isLiked = !work.isLiked;
    work.likeCount = work.isLiked ? work.likeCount + 1 : work.likeCount - 1;
    
    this.setData({
      work: work
    });
  },

  // 评论点赞
  likeComment(e) {
    const index = e.currentTarget.dataset.index;
    const work = { ...this.data.work };
    const comment = work.comments[index];
    
    comment.isLiked = !comment.isLiked;
    comment.likeCount = comment.isLiked ? comment.likeCount + 1 : comment.likeCount - 1;
    
    this.setData({
      work: work
    });
  },

  // 评论输入
  onInputChange(e) {
    this.setData({
      commentContent: e.detail.value
    });
  },

  // 发送评论
  sendComment() {
    if (!this.data.commentContent.trim()) {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }

    const work = { ...this.data.work };
    const newComment = {
      id: Date.now(),
      avatar: 'https://picsum.photos/100/100?random=999',
      username: '我',
      time: '刚刚',
      content: this.data.commentContent,
      likeCount: 0,
      isLiked: false
    };

    work.comments.unshift(newComment);
    work.commentCount += 1;
    
    this.setData({
      work: work,
      commentContent: '',
      showCommentInput: false
    });

    wx.showToast({
      title: '评论成功',
      icon: 'success'
    });
  },

  // 显示评论输入框
  showCommentInput() {
    this.setData({
      showCommentInput: true
    });
  },

  // 预览图片
  previewImage(e) {
    const current = e.currentTarget.dataset.url;
    const urls = this.data.work.images;
    
    wx.previewImage({
      current: current,
      urls: urls
    });
  },

  // 分享
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  // 分享给朋友
  onShareAppMessage() {
    return {
      title: this.data.work.content,
      path: `/my/pages/work-detail/work-detail?id=${this.data.work.id}`,
      imageUrl: this.data.work.images[0]
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: this.data.work.content
    };
  }
}); 